package controller;

import com.sun.org.glassfish.external.probe.provider.annotations.ProbeParam;
import model.Book;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.multipart.MultipartFile;
import service.BookService;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping(value = "book")
public class BookController extends BaseController{
    private BookService bookService;

    public BookController(BookService bookService) {
        this.bookService = bookService;
    }
    //查询全部图书
    @RequestMapping(value = "queryAllBooks/{role}",method = RequestMethod.GET)
    private String queryAllBooks(@PathVariable("role") int role){
        session.setAttribute("books",bookService.queryAll());
        if (role == 1){
            return "redirect:/book/admin/index.jsp";
        }
        if (role == 0){
            return "redirect:/book/user/index.jsp";
        }
        return " ";
    }
    //删除图书
    @RequestMapping(value = "removeBook/{id}",method = RequestMethod.GET)
    private String removeBook(@PathVariable("id") int id){
        bookService.remove(id);
        session.setAttribute("message","图书Id为"+id+"的图书已被删除！");
        session.setAttribute("books",bookService.queryAll());
        return "redirect:/book/admin/index.jsp";
    }

    //图书详情
    @RequestMapping(value = "bookDetail/{id}",method = RequestMethod.GET)
    private String queryBookDetail(@PathVariable("id") int id){
        session.setAttribute("book",bookService.queryById(id));
        return "redirect:/book/admin/book_detail.jsp";
    }
    //添加图书
    @RequestMapping(value = "addBook",method = RequestMethod.POST)
    private String addBook(Book book, MultipartFile file) throws IOException {
        if (file ==null){
            book.setCover("default.jpg");
        }else if (file != null){
            File dir= new File(application.getRealPath("/cover"));
            String filename  = UUID.randomUUID()+"."+ StringUtils.getFilenameExtension(file.getOriginalFilename());
            file.transferTo(new File(dir,filename));
            book.setCover(filename);
        }
        bookService.create(book);
        session.setAttribute("message","添加图书成功！");
        session.setAttribute("books",bookService.queryAll());
        return "redirect:/book/admin/index.jsp";
    }
    //编辑 step01 查询需要的信息并显示在输入框中
    @RequestMapping(value = "editerStep1/{id}",method = RequestMethod.GET)
    private String editerStep1(@PathVariable("id") int id ){
        Book book =  bookService.queryById(id);
        session.setAttribute("book",book);
        return "redirect:/book/admin/editer.jsp";
    }
    //编辑 step02 更新修改
    @RequestMapping(value = "editerStep2",method = RequestMethod.POST)
    private String editerStep2(Book book,MultipartFile file) throws IOException {
        File dir= new File(application.getRealPath("/cover"));
        String filename  = UUID.randomUUID()+"."+ StringUtils.getFilenameExtension(file.getOriginalFilename());
        file.transferTo(new File(dir,filename));
        book.setCover(filename);
        bookService.modify(book);
        session.setAttribute("message","更新图书状态成功！");
        session.setAttribute("books",bookService.queryAll());
        return "redirect:/book/admin/index.jsp";
    }
}
